package cn.wolfcode.trip.base.service.impl;

import cn.wolfcode.trip.base.domain.Answer;
import cn.wolfcode.trip.base.mapper.AnswerMapper;
import cn.wolfcode.trip.base.query.AnswerQuery;
import cn.wolfcode.trip.base.service.IAnswerService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class AnswerServiceImpl implements IAnswerService {
    @Autowired
    private AnswerMapper answerMapper;

    @Override
    public PageInfo selectAnswerByQueestionId(AnswerQuery qo,Long questionId) {
        qo.setOrderBy("an.answerTime DESC");
        PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize(),qo.getOrderBy());
        List<Answer> list = answerMapper.selectAnswerByQueestionId(qo,questionId);
        return new PageInfo(list);
    }


    @Override
    public List<Answer> listAll() {
        return answerMapper.selectAll();
    }

    @Override
    public Answer get(Long id) {
        answerMapper.updateViewsNums(id);
        return answerMapper.selectByPrimaryKey(id);
    }

    @Override
    public void saveReplyByQuestionId(Answer answer, Long questionId) {
        answerMapper.insert(answer,questionId);
    }

    @Override
    public int countAnswerNums(Long questionId) {
        return answerMapper.countAnswerNums(questionId);
    }

}
